import datetime


def syslog(logfile, log, show=False):
    # 获取当前时间
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    # 格式化日志信息，包含时间戳
    formatted_log = f"{now} - {log}\n"
    # 尝试以追加模式打开文件
    try:
        with open(logfile, 'a') as f:
            f.write(formatted_log)
            # 如果show为True，则打印日志并添加行号
        if show:
            line_number = 1
            # 去除换行符以简化显示
            print(f"{line_number}： {formatted_log.strip()}")
    except Exception as e:
        print(f"Error writing to logfile: {e}")


# 调用函数，输出结果
syslog('example.log', 'This is a test log message', show=True)
syslog('example.log', 'Another test log message', show=True)